package com.ffcs.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import open189.sign.ParamsSign;
import net.sf.json.JSONObject;

import com.ffcs.constants.FfcsConstants;
import com.smart.util.WeixinUtil;

//获取信任码
public class TokenManager {
	public static String  getToken() throws UnsupportedEncodingException
	{
	 //先获取到令牌接口
	 String ACCESS_TOKEN=RandomAccessTokenManager.getRandomAccessToken();
	 Date date = new Date();
	 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	 String timestamp = dateFormat.format(date);
	 System.out.println(timestamp);
	 TreeMap<String, String> paramsMap = new TreeMap<String, String>();
	 paramsMap.put("app_id",FfcsConstants.APPID);
	 paramsMap.put("access_token", ACCESS_TOKEN);
	 paramsMap.put("timestamp", timestamp);
	
	 String getUrl = FfcsConstants.TOKEN_URL+"?app_id=" + FfcsConstants.APPID
			+ "&access_token=" +ACCESS_TOKEN 
			+ "&timestamp="+URLEncoder.encode(timestamp,"UTF-8") 
			+ "&sign="+ParamsSign.value(paramsMap, FfcsConstants.APPSECRET);
	System.out.println(getUrl);
	JSONObject jsonObject=WeixinUtil.doGetStr(getUrl);
	System.err.println(jsonObject);
	String res_code=jsonObject.getString("res_code");
	String token=jsonObject.getString("token");
	System.out.println("res_code:"+res_code+","+"token:"+token);
	return token;
  }
	
 public static void main(String[] args)
 {
	 try {
		TokenManager.getToken();
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
 }
}
